﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DALC4NET;
using NLog;
using System.Data;
using Entity.Client;

namespace DAL
{
    public class RegUsers
    {
        #region Variables
        private static Logger _logger = LogManager.GetCurrentClassLogger();
        DBHelper _dbHelper = null;
        #endregion

        #region Constacter
        public RegUsers()
        {
            _dbHelper = DBHelper.GetInstance();
        }
        #endregion

        #region Methods

        public int InsertNewRegisterUser(RegisterUsers objRegisterUsers)
        {
            IDbCommand command = null;
            int retValue = 0;

            try
            {

                DBParameter param1 = new DBParameter("@facebookID", objRegisterUsers.FacebookID);
                DBParameter param2 = new DBParameter("@userFullName", objRegisterUsers.UserFullName);
                DBParameter param3 = new DBParameter("@userEmailAddress", objRegisterUsers.EmailAddress);
                DBParameter param4 = new DBParameter("@userPassword", objRegisterUsers.Password);
                DBParameter param5 = new DBParameter("@userCountry", objRegisterUsers.Country);
                DBParameter param6 = new DBParameter("@userStatus", objRegisterUsers.UserStatus);
                DBParameter param7 = new DBParameter("@type", objRegisterUsers.UserStatus);
                DBParameter param8 = new DBParameter("@AuthType", objRegisterUsers.UserStatus);

                DBParameter param9 = new DBParameter("@userAboutUser", objRegisterUsers.UserBio);
                DBParameter param10 = new DBParameter("@userImageProfile", objRegisterUsers.UserImageProfile);

                DBParameter outParam = new DBParameter("@newId", 0, DbType.Int16, ParameterDirection.Output);

                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                paramCollection.Add(param2);
                paramCollection.Add(param3);
                paramCollection.Add(param4);
                paramCollection.Add(param5);
                paramCollection.Add(param6);
                paramCollection.Add(param7);
                paramCollection.Add(param8);

                paramCollection.Add(param9);
                paramCollection.Add(param10);

                paramCollection.Add(outParam);


                command = _dbHelper.GetCommand("InsertNewRegisterUser", paramCollection, CommandType.StoredProcedure);
                command.ExecuteNonQuery();
                return Convert.ToInt16(_dbHelper.GetParameterValue(10, command));
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::RegUsers:::InsertNewContactUs:::" + ex.Message);
                return 0;
                throw;
            }
        }

        public int EditClientInfo(RegisterUsers objRegisterUsers)
        {
            try
            {
                int rowsAffected = 0;
                DBParameter param1 = new DBParameter("@facebookID", objRegisterUsers.FacebookID);
                DBParameter param2 = new DBParameter("@userFullName", objRegisterUsers.UserFullName);
                DBParameter param3 = new DBParameter("@userEmailAddress", objRegisterUsers.EmailAddress);
                DBParameter param4 = new DBParameter("@userPassword", objRegisterUsers.Password);
                DBParameter param5 = new DBParameter("@userCountry", objRegisterUsers.Country);
                DBParameter param6 = new DBParameter("@userStatus", objRegisterUsers.UserStatus);
                DBParameter param7 = new DBParameter("@type", objRegisterUsers.UserStatus);
                DBParameter param8 = new DBParameter("@AuthType", objRegisterUsers.UserStatus);
                DBParameter param9 = new DBParameter("@userID", objRegisterUsers.UserID);

                DBParameter param10 = new DBParameter("@userAboutUser", objRegisterUsers.UserBio);
                DBParameter param11 = new DBParameter("@userImageProfile", objRegisterUsers.UserImageProfile);

                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                paramCollection.Add(param2);
                paramCollection.Add(param3);
                paramCollection.Add(param4);
                paramCollection.Add(param5);
                paramCollection.Add(param6);
                paramCollection.Add(param7);
                paramCollection.Add(param8);
                paramCollection.Add(param9);
                paramCollection.Add(param10);
                paramCollection.Add(param11);

                rowsAffected = _dbHelper.ExecuteNonQuery("sp_EditClientInfo", paramCollection, CommandType.StoredProcedure);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::EditClientInfo:::" + ex.Message);
                return 0;
            }
        }
        public int DeleteRegisterUser(string sUserID)
        {
            try
            {
                int rowsAffected = 0;
                DBParameter param1 = new DBParameter("@userID", sUserID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                rowsAffected = _dbHelper.ExecuteNonQuery("sp_DeleteRegisterUser", paramCollection, CommandType.StoredProcedure);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::RegUsers:::DeleteRegisterUser:::" + ex.Message);
                throw;
            }
        }
        public int BanndUser(string sUserID, string sUserStatus)
        {
            try
            {
                int rowsAffected = 0;
                DBParameter param1 = new DBParameter("@userID", sUserID);
                DBParameter param2 = new DBParameter("@userStatus", sUserStatus);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                paramCollection.Add(param2);
                rowsAffected = _dbHelper.ExecuteNonQuery("sp_BanndUser", paramCollection, CommandType.StoredProcedure);
                return rowsAffected;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::RegUsers:::BanndUser:::" + ex.Message);
                throw;
            }
        }
        public DataSet GetAllRegisterUsers()
        {
            try
            {
                DataSet dsReturnAllUser = _dbHelper.ExecuteDataSet("sp_GetAllRegisterUsers", CommandType.StoredProcedure);
                return dsReturnAllUser;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::RegUsers:::GetAllRegisterUsers:::" + ex.Message);
                throw;
            }
        }
        public RegisterUsers GetRegisterUsersByUserID(string sUserID)
        {
            IDbConnection connection = _dbHelper.GetConnObject();
            IDataReader reader = null;
            RegisterUsers objRegisterUsers = new RegisterUsers();
            try
            {
                DBParameter param1 = new DBParameter("@userID", sUserID);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);

                reader = _dbHelper.ExecuteDataReader("sp_GetRegisterUsersByUserID", connection, paramCollection, CommandType.StoredProcedure);
                while (reader.Read())
                {
                    objRegisterUsers.UserFullName = reader["UserFullName"].ToString();
                    objRegisterUsers.EmailAddress = reader["UserEmailAddress"].ToString();
                    objRegisterUsers.Password = reader["UserPassword"].ToString();
                    objRegisterUsers.FacebookID = reader["FacebookID"].ToString();
                    objRegisterUsers.UserBio = reader["UserAboutUser"].ToString();
                    objRegisterUsers.UserImageProfile = reader["UserImageProfile"].ToString();
                }
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::GetArticlsInfo:::" + ex.Message);
                objRegisterUsers = null;
            }
            return objRegisterUsers;

        }
        public int CheckUserRegisterFacebook(string userFacebookID)
        {
            IDbCommand command = null;
            object retValue = null;
            try
            {
                DBParameter param1 = new DBParameter("@facebookID", userFacebookID);
                DBParameter outParam = new DBParameter("@returnValue", 0, DbType.Int16, ParameterDirection.Output);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                paramCollection.Add(outParam);

                command = _dbHelper.GetCommand("sp_CheckFacebookRegister", paramCollection, CommandType.StoredProcedure);
                command.ExecuteNonQuery();
                retValue = _dbHelper.GetParameterValue(1, command);
                return Convert.ToInt16(retValue);

            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::RegUsers:::CheckUserRegisterFacebook:::" + ex.Message);
                throw ex;
            }
        }
        public Int32 LoginClientUser(RegisterUsers objRegisterUsers)
        {
            IDbConnection connection = _dbHelper.GetConnObject();
            IDataReader reader = null;
            Int32 nUserID = 0;
            try
            {
                DBParameter param1 = new DBParameter("@username", objRegisterUsers.EmailAddress);
                DBParameter param2 = new DBParameter("@password", objRegisterUsers.Password);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);
                paramCollection.Add(param2);
                reader = _dbHelper.ExecuteDataReader("sp_ClientUserLogin", connection, paramCollection, CommandType.StoredProcedure);
                while (reader.Read())
                {
                    nUserID = Convert.ToInt32(reader["UserID"].ToString());
                }
                return nUserID;
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::UserAuthentication:::ClientLoginUser:::" + ex.Message);
                return -1;
            }
        }
        public RegisterUsers RetriveUserPasswordByEmailAddress(string sUserEmailAddress)
        {
            IDbConnection connection = _dbHelper.GetConnObject();
            IDataReader reader = null;
            RegisterUsers objRegisterUsers = new RegisterUsers();
            try
            {
                DBParameter param1 = new DBParameter("@emailAddress", sUserEmailAddress);
                DBParameterCollection paramCollection = new DBParameterCollection();
                paramCollection.Add(param1);

                reader = _dbHelper.ExecuteDataReader("sp_RetriveUserPasswordByEmailAddress", connection, paramCollection, CommandType.StoredProcedure);
                while (reader.Read())
                {
                    objRegisterUsers.EmailAddress = reader["UserEmailAddress"].ToString();
                    objRegisterUsers.Password = reader["UserPassword"].ToString(); 
                }
            }
            catch (Exception ex)
            {
                _logger.Error("DAL:::ManageArticls:::RetriveUserPasswordByEmailAddress:::" + ex.Message);
                objRegisterUsers = null;
            }
            return objRegisterUsers;

        }
        #endregion
    }
}
